/**
 * Created by meiguizhinian on 2018/6/28
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//对象 let obj = new ll.Obj(param)
(function () {
	ivy.AnimationData = function (filename) {
		let is = new ivy.InputStream(filename);

		this.mapKeyFrameBaseData_ = new LLDictionary();
		this.mapKeyFrameCustomData_ = new LLDictionary();
		this.mapKeyFrameCustomZoneData_ = new LLDictionary();
		if (!is.empty()) {
			this.initWithStream(is);
		}
	};
	let p = ivy.AnimationData.prototype;
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	p.initWithStream = function(is) {
		this.aniType_ = is.readInt8();
		this.mlgCount_ = is.readUInt8();
		this.srcImgNames_ = [];
		this.suitImgNames_ = [];
		for (let i = 0; i < this.mlgCount_; ++i) {
			this.srcImgNames_[i] = is.readStringUTF8();
			let suitCount = is.readInt8();
			if (suitCount !== 0) {
				this.suitImgNames_[i] = [];
				for (let j = 0; j < suitCount; ++j) {
					this.suitImgNames_[i][j] = is.readStringUTF8();
				}
			}
		}
		this.moduleCount_ = is.readJUInt16();
		this.moduleImgIds_ = [];
		this.moduleRects_ = [];
		for (let i = 0; i < this.moduleCount_; ++i) {
			this.moduleImgIds_[i] = is.readInt8();
			let x = is.readJInt16();
			let y = is.readJInt16();
			let w = is.readJInt16();
			let h = is.readJInt16();
			this.moduleRects_[i] = cc.rect(x,y,w,h);
		}

		this.templateCount_ = is.readJInt16();
		for (let i = 0; i < this.templateCount_; ++i) {
			is.readInt8();
		}
		for (let i = 0; i < this.templateCount_+1; ++i) {
			is.readJInt16();
		}
		this.templateSpriteCount_ = is.readJInt16();
		for (let i = 0; i < this.templateSpriteCount_; ++i) {
			is.readJInt32();
		}

		this.frameCount_ = is.readJInt16();
		this.frameBoxIndexs_ = [];
		is.readUInt8Array(this.frameBoxIndexs_, this.frameCount_<<1);
		this.frameSpriteCounts_ = [];
		is.readUInt8Array(this.frameSpriteCounts_, this.frameCount_);
		for (let i = 0; i < this.frameSpriteCounts_.length; i++) {
			if (this.frameSpriteCounts_[i]>this.frameSpriteMaxCount_) {
				this.frameSpriteMaxCount_ = this.frameSpriteCounts_[i];
			}
		}
		this.frameSpriteOffset_ = [];
		for (let i = 0; i < this.frameCount_ + 1; ++i) {
			this.frameSpriteOffset_[i] = is.readJUInt16();
		}

		//Frame sprite
		this.spritesCount_ = is.readJInt16();
		this.spriteFlips_ = [];
		this.spriteModules_ = [];
		this.spritePositions_ = [];
		this.spriteParticles_ = [];
		this.spriteEffects_ = [];
		this.spriteRotateIndexs_ = [];
		this.spriteSkeletons_ = [];
		for (let i = 0; i < this.spritesCount_; i++)
		{
			this.spriteFlips_[i] = is.readInt8();
			this.spriteModules_[i] = is.readJInt16();
			let x = is.readJInt16();
			let y = is.readJInt16();
			this.spritePositions_[i] = cc.p(x, -y);
			this.spriteParticles_[i] = is.readJInt16();
			this.spriteEffects_[i] = is.readJInt16();
			this.spriteRotateIndexs_[i] = is.readJInt16();
			this.spriteSkeletons_[i] = is.readJInt16();
		}
		//Action
		this.actionCount_ = is.readJInt16();
		this.actionMechModelIndexs_ = [];
		is.readInt8Array(this.actionMechModelIndexs_, this.actionCount_);
		this.actionSequenceCounts_ = [];
		is.readUInt8Array(this.actionSequenceCounts_, this.actionCount_);
		this.actionSequenceOffset_= [];
		for (let i = 0; i < this.actionCount_ + 1; ++i) {
			this.actionSequenceOffset_[i] = is.readJInt16();
		}
		//Action sequence
		this.sequenceCount_=is.readJInt16();
		this.seqDurations_ = [];
		this.seqFrameIds_ = [];
		this.seqSoundIds_ = [];
		this.seqReserve1Values_ = [];
		this.seqFlyDatas_ = [];
		this.frameHadDataNeedToDealVec_ = [];
		let hurtStateData, stiffFrameCountsData, moveDistanceData, reserveData;
		for (let i = 0; i < this.sequenceCount_; i++) {
			let needDeal = false;
			hurtStateData = is.readJInt16();
			stiffFrameCountsData = is.readJInt16();
			moveDistanceData = is.readJInt16();
			reserveData = is.readJInt16();
			if (hurtStateData !== 0) {
				let resultID = i << 16 | ivy.KeyFrameBaseType.eHurtState;
				this.mapKeyFrameBaseData_.set(resultID, hurtStateData);
				needDeal = true;
			}
			if (stiffFrameCountsData !== 0) {
				let resultID = i << 16 | ivy.KeyFrameBaseType.eStiffFrame;
				this.mapKeyFrameBaseData_.set(resultID, stiffFrameCountsData);
				needDeal = true;
			}
			if (moveDistanceData !== 0) {
				let resultID = i << 16 | ivy.KeyFrameBaseType.eMoveDis;
				this.mapKeyFrameBaseData_.set(resultID, moveDistanceData);
				needDeal = true;
			}
			if (reserveData !== 0) {
				let resultID = i << 16 | ivy.KeyFrameBaseType.eReverse;
				this.mapKeyFrameBaseData_.set(resultID, reserveData);
				needDeal = true;
			}
			this.seqDurations_[i] = is.readUInt8();
			this.seqFrameIds_[i] = is.readJInt16();
			this.seqSoundIds_[i] = is.readJInt16();
			//添加2个short导入 暂时编辑器没有导出该数据
			this.seqReserve1Values_[i] = is.readJInt16();
			this.seqFlyDatas_[i] = is.readJInt16();

			//添加关键帧数据导入
			let customKeyFrameDataCount = is.readJInt16();
			//此处*2结构为ID-Value
			for (let index = 0; index < customKeyFrameDataCount; index++) {
				let id = is.readJInt16();
				let value = is.readJInt16();
				let resultID = i << 16 | id;
				this.mapKeyFrameCustomData_.set(resultID, value);
				needDeal = true;
			}

			//添加关键区域数据导入
			let customKeyFrameZoneDataCount = is.readJInt16();
			//此处*5结构为ID-X-Y-W-H
			for (let index = 0; index < customKeyFrameZoneDataCount; index++) {
				let id = is.readJInt16();
				let x = is.readJInt16();
				let y = is.readJInt16();
				let w = is.readJInt16();
				let h = is.readJInt16();
				needDeal = true;
				let resultID = i << 16 | id;
				this.mapKeyFrameCustomZoneData_.set(resultID, cc.rect(x, y, w, h));
			}
			this.frameHadDataNeedToDealVec_[i]=needDeal;
		}
		//Box
		this.colBoxCount_ = is.readJInt16();
		this.attBoxCount_ = is.readJInt16();
		this.colRects_ = [];
		this.attRects_ = [];
		for (let i = 0; i < this.colBoxCount_; i++) {
			let x1 = is.readJInt16();
			let y1 = is.readJInt16();
			let x2 = is.readJInt16();
			let y2 = is.readJInt16();
			this.colRects_[i] = cc.rect(x1, y1, x2 - x1, y2 - y1);
		}
		for (let i = 0; i < this.attBoxCount_; i++) {
			let x1 = is.readJInt16();
			let y1 = is.readJInt16();
			let x2 = is.readJInt16();
			let y2 = is.readJInt16();
			this.attRects_[i] = cc.rect(x1, y1, x2 - x1, y2 - y1);
		}
		//Mech model
		this.mechCount_ = is.readJInt16();
		this.mechModels_ = [];
		is.readUInt8Array(this.mechModels_, this.mechCount_*5);
		//Rotate
		this.transformSpriteCount_ = is.readJInt16();
		this.transformRotations_ = [];
		this.transformScaleXs_ = [];
		this.transformScaleYs_ = [];
		this.transformOpacitys_ = [];
		this.transformColors_ = [];
		for (let i = 0; i < this.transformSpriteCount_; ++i) {
			this.transformRotations_[i] = is.readJInt16();
			this.transformScaleXs_[i] = is.readJInt32() / 100.0;
			this.transformScaleYs_[i] = is.readJInt32() / 100.0;
			let argb = is.readJInt32();
			this.transformOpacitys_[i] = ivy.Tools.getAlphaFromInt32(argb);
			this.transformColors_[i] = ivy.Tools.getColor3BFromInt32(argb);
		}
		return true;
	};
	//获取当前动画数据最多用到的图元数
	p.getFrameSpriteMaxCount = function() {
		return this.frameSpriteMaxCount_;
	};

	p.getSequenceDuration = function(seqID) {
		return this.seqDurations_[seqID];
	};

	p.getActionSequenceOffset = function(actionID) {
		return this.actionSequenceOffset_[actionID];
	};

	p.getActionSequenceCount = function(actionID) {
		return this.actionSequenceCounts_[actionID];
	};

	p.getSequenceIndex = function(actionID, sequenceID) {
		return this.actionSequenceOffset_[actionID] + sequenceID;
	};

	p.getActionCount = function() {
		return this.actionCount_;
	};

	p.getFrameID = function(actionID, sequenceID) {
		if (actionID >= 0 && actionID < this.actionCount_) {
			return this.seqFrameIds_[this.getSequenceIndex(actionID, sequenceID)];
		}
		return 0;
	};

	p.getFrameSpriteOffset = function(frameID) {
		return this.frameSpriteOffset_[frameID];
	};

	p.getFrameSpriteCount = function(frameID) {
		return this.frameSpriteCounts_[frameID];
	};

	p.getSpriteFlip = function(spriteIndex) {
		return this.spriteFlips_[spriteIndex];
	};

	p.getSpritePosition = function(spriteIndex) {
		return this.spritePositions_[spriteIndex];
	};

	p.getSpriteExDataIndex = function(spriteIndex) {
		return this.spriteRotateIndexs_[spriteIndex];
	};

	p.getSpriteModuleID = function(spriteIndex) {
		return this.spriteModules_[spriteIndex];
	};

	p.getModuleImgID = function(moduleID) {
		return this.moduleImgIds_[moduleID];
	};

	p.getModuleRect = function(moduleID) {
			return this.moduleRects_[moduleID];
	};

	p.getSrcImageName = function(imgID) {
		return this.srcImgNames_[imgID];
	};

	p.isSuitIDVaild = function(imgID, suitID) {
		if (imgID >= 0 && imgID < this.suitImgNames_.length) {
			if (suitID >= 0 && suitID < this.suitImgNames_[imgID].length) {
				return true;
			}
		}
		return false;
	};

	p.getSuitImageName = function(imgID, suitID) {
		return this.suitImgNames_[imgID][suitID];
	};

	p.getTransformRotation = function(transformID) {
		return this.transformRotations_[transformID];
	};

	p.getTransformScaleX = function(transformID) {
		return this.transformScaleXs_[transformID];
	};

	p.getTransformScaleY = function(transformID) {
		return this.transformScaleYs_[transformID];
	};

	p.getTransformOpacity = function(transformID) {
		return this.transformOpacitys_[transformID];
	};

	p.getTransformColor = function(transformID) {
		return this.transformColors_[transformID];
	};

	p.getSoundID = function(actionID, seqID) {
		return this.seqSoundIds_[this.actionSequenceOffset_[actionID] + seqID];
	};

	p.getKeyFrameBaseData = function(actionID, seqID, type) {
		let realSeqID = this.actionSequenceOffset_[actionID] + seqID;
		let findV = realSeqID << 16 | type;
		return this.mapKeyFrameBaseData_.get(findV) || 0;
	};
	p.getKeyFrameCustomData = function(actionID, seqID, type) {
		let realSeqID = this.actionSequenceOffset_[actionID] + seqID;
		let findV = realSeqID << 16 | type;
		return this.mapKeyFrameCustomData_.get(findV) || 0;
	};
	p.getKeyFrameCustomZoneData = function(actionID, seqID, type) {
		let realSeqID = this.actionSequenceOffset_[actionID] + seqID;
		let findV = realSeqID << 16 | type;
		return this.mapKeyFrameCustomZoneData_.get(findV) || cc.rect(-1,-1,-1,-1);
	};
	p.isKeyFrameBaseDataExist = function(actionID, seqID, type) {
		let realSeqID = this.actionSequenceOffset_[actionID] + seqID;
		let findV = realSeqID << 16 | type;
		return this.mapKeyFrameBaseData_.has(findV);
	};
	p.isKeyFrameCustomDataExist = function(actionID, seqID, type) {
		if (actionID < 0 || seqID < 0 || type < 0)
			return false;
		if (actionID >= this.actionSequenceOffset_.length)
			return false;
		let realSeqID = this.actionSequenceOffset_[actionID] + seqID;
		let findV = realSeqID << 16 | type;
		return this.mapKeyFrameCustomData_.has(findV);
	};
	p.isKeyFrameCustomZoneDataExist = function(actionID, seqID, type) {
		if (actionID >= this.actionSequenceOffset_.length)
			return false;
		let realSeqID = this.actionSequenceOffset_[actionID] + seqID;
		let findV = realSeqID << 16 | type;
		return this.mapKeyFrameCustomZoneData_.has(findV);
	};
	p.getAnimationName = function() {
		return this.animationName_;
	};

	p.setAnimationName = function(name){
		this.animationName_ = name;
	};

	p.isKeyFrame = function(actionID, seqID) {
		if (actionID < 0 || actionID >= this.actionSequenceOffset_.length) {
			return false;
		}
		return !!this.frameHadDataNeedToDealVec_[this.actionSequenceOffset_[actionID] + seqID];
	};

	p.getAttackBox = function(actionID, seqID) {
		let frameId = this.getFrameID(actionID, seqID);
		return this.attRects_[this.frameBoxIndexs_[(frameId<<1)+1]];
	};
	p.getCollisionBox = function(actionID, seqID) {
		let frameId = this.getFrameID(actionID, seqID);
		return this.colRects_[this.frameBoxIndexs_[(frameId << 1)]];
	};
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////private
	p.aniType_ = 0;
	p.mlgCount_ = 0;

	p.srcImgNames_ = null;//[]
	p.suitImgNames_ = null;//[]

	p.templateCount_ = 0;
	p.templateSpriteCount_ = 0;

	p.moduleCount_ = 0;
	p.moduleImgIds_ = null;//[]
	p.moduleRects_ = null;//[]

	p.frameCount_ = 0;
	p.frameBoxIndexs_ = null;//[]
	p.frameSpriteCounts_ = null;//[]
	p.frameSpriteOffset_ = null;//[]
	p.frameSpriteMaxCount_ = 0;

	p.spritesCount_ = 0;

	p.spriteFlips_ = null;//[]
	p.spriteModules_ = null;//[]
	p.spritePositions_ = null;//[]
	p.spriteParticles_ = null;//[]
	p.spriteEffects_ = null;//[]
	p.spriteRotateIndexs_ = null;//[]
	p.spriteSkeletons_ = null;//[]

	p.actionCount_ = 0;
	p.actionMechModelIndexs_ = null;//[]
	p.actionSequenceCounts_ = null;//[]
	p.actionSequenceOffset_ = null;//[]

	p.sequenceCount_ = 0;
	p.seqDurations_ = null;//[]
	p.seqFrameIds_ = null;//[]
	p.seqSoundIds_ = null;//[]
	p.seqReserve1Values_ = null;//[]
	p.seqFlyDatas_ = null;//[]

	p.frameHadDataNeedToDealVec_ = null;//[]
	p.mapKeyFrameBaseData_ = null;//{}
	p.mapKeyFrameCustomData_ = null;//{}
	p.mapKeyFrameCustomZoneData_ = null;//{}

	p.colBoxCount_ = 0;
	p.attBoxCount_ = 0;
	p.colRects_ = null;//{}
	p.attRects_ = null;//{}

	p.mechCount_ = 0;
	p.mechModels_ = null;//[]

	p.transformSpriteCount_ = 0;
	p.transformRotations_ = null;//std::vector<int16_t>
	p.transformScaleXs_ = null;//std::vector<float>
	p.transformScaleYs_ = null;//std::vector<float>
	p.transformOpacitys_ = null;//std::vector<int32_t>
	p.transformColors_ = null;//std::vector<cocos2d::Color3B>

	p.animationName_ = "";
})();

ivy.AnimationData.AnimationType = {
	eNormal : 0,
	eSpine  : 1,
};

ivy.AnimationData.KeyFrameBaseType = {
	//受伤状态
	eHurtState  : 0,
	//僵直帧
	eStiffFrame : 1,
	//强制移动距离
	eMoveDis    : 2,
	//备用
	eReverse    : 3,
};

ivy.AnimationData.createWithFileName = function (filename) {
	return new ivy.AnimationData(filename);
};

ivy.AnimationData.particles = [];// 动画关联粒子索引